package com.feihong.txgw.tx_precenter;

import com.alibaba.fastjson.JSON;
import com.feihong.txgw.inter.contract.order.IReviewOrderContract;
import com.feihong.txgw.json.user.ProductOrderDetailEntity;
import com.feihong.txgw.tx_model.ReviewOrderMainModel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * @Author wangziheng
 * @ProjectName txgw_app
 * @ClassName ReviewMainPrecenter
 * @package com.feihong.txgw.tx_precenter
 * @data 2018/5/7 13:58
 * @Describe
 * @Change
 */
public class ReviewMainPrecenter extends IReviewOrderContract.AbReviewOrderMainPrecenter {
    @Override
    public void loadOrderGoodsInfo(String tableID, String orderID) {
        mIModel.getOrderGoodsInfo(tableID, orderID).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                try {
                    List<ProductOrderDetailEntity> mDetailList = new ArrayList<ProductOrderDetailEntity>();
                    JSONObject jsonObject = new JSONObject(o);
                    JSONArray jsonArray = jsonObject.optJSONArray("data");
                    if("200".equals(jsonObject.optString("code"))){
                        if(jsonArray != null){
                            for(int i = 0 ; i < jsonArray.length() ; i++){
                                ProductOrderDetailEntity productOrderDetailEntity = JSON.parseObject(jsonArray.optJSONObject(i).toString(),ProductOrderDetailEntity.class);
                                mDetailList.add(productOrderDetailEntity);
                            }
                        }
                        mIView.getOrderGoodsResult(mDetailList);
                    }else {
                        mIView.showSystemToast(jsonObject.optString("msg"));
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable e) {
                mIView.getOrderGoodsError();
            }

            @Override
            public void onComplete() {
                mIView.loadDataComplated();
            }
        });
    }

    @Override
    public void submitOrderReview(String orderSn, String type, String delivery_rand, String serviceRand, String goodsInfo) {

        mIModel.submitReview(orderSn, type, delivery_rand, serviceRand, goodsInfo).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                mRxManager.register(d);
            }

            @Override
            public void onNext(String o) {
                mIView.submitOrderReviewResult();
            }

            @Override
            public void onError(Throwable e) {
                mIView.submitOrderReviewError();
            }

            @Override
            public void onComplete() {

                mIView.loadDataComplated();
            }
        });
    }

    public static ReviewMainPrecenter create(){
        return new ReviewMainPrecenter();
    }
    @Override
    public IReviewOrderContract.IReviewOrderMainModel getModel() {
        return ReviewOrderMainModel.create();
    }
    @Override
    public void onStart() {

    }
}
